* Party-Determined Viability and Gender Bias in Open-List Proportional Representation Systems
* By Rimvydas Ragauskas rimvydas.ragauskas@ttu.edu

* Clean .do file; summary statistics, diagnostics and alternative specifications are excluded
* Only code for generation of reported tables and graphs


*********************************************************************
*********************** Supply side: Models 1-2**********************
*********************************************************************

* Addressing nomination paterns
* MODEL 1
reg revrank_be sex exp prev_tenures_excl minist_inc prev_min_excl  cand_smd1 el_one el_two el_three i.p_code, robust /* with robust holds */
estat ic

* Driven by SDP 
reg revrank_be sex exp prev_tenures_excl minist_inc prev_min_excl  cand_smd1 el_one el_two el_three i.p_code if p_code!=1, robust
estat ic

* Sex and exp - rather meritocratic paterns of nomination
reg revrank_be sex##exp sex##c.prev_tenures_excl sex##minist_inc sex##c.prev_min_excl sex##cand_smd1 el_one el_two el_three i.p_code
estat ic


* MODEL 2: by parties
reg revrank_be sex##SDP sex##conserv sex##lib sex##tt sex##polish sex##dp sex##dk sex##lcs sex##tautp sex##greens exp prev_tenures_excl minist_inc prev_min_excl cand_smd1 el_one el_two el_three i.p_code, robust
estat ic




*********************************************************************
********* Demand side: Models 3-5 + robbusness models with RE *******
*********************************************************************

* Model 3
nbreg pv sex exp prev_tenures_excl minist_inc prev_min_excl revrank_be cand_smd1 el_one el_two el_three i.p_code, irr
estat ic

* Model 6 (robbustnes appendix)
nbreg pv sex##p_elect exp prev_tenures_excl minist_inc prev_min_excl revrank_be cand_smd1 el_one el_two el_three i.p_code, irr
margins, at(sex=(0 1)) atmeans
* Model 7 (robbustnes appendix)
menbreg pv sex##p_elect exp prev_tenures_excl minist_inc prev_min_excl revrank_be cand_smd1 el_one el_two el_three || p_code:, irr



* Model 4: Rank on the list
* Unconditional fixed
nbreg pv sex##c.revrank_be exp prev_tenures_excl minist_inc prev_min_excl cand_smd1 el_one el_two el_three i.p_code, irr
estat ic
* Figure 1. Predictions; gaps drawn manually
set scheme lean2
margins, at(revrank_be=(1(10)141) exp=(1) minist_inc=(0) cand_smd1=(1)) over(sex) atmeans /* gap calculated from this output */
marginsplot
marginsplot, yla(, nogrid) recast(scatter) ///
	ytitle("Predidcted Numb. of PVs Given For Candidate in the District", size(vsmall)) ///
	xtitle("Rank in the List", size(small)) ///
	title("Figure 1: Predicted Vote Count Depending on Rank in the List", span size(medium)) ///
	legend(row(1) position(6))

* Model 9 (robbustnes appendix): Multilevel (for robbustness if you want to show)
menbreg pv sex##c.revrank_be exp prev_tenures_excl minist_inc prev_min_excl cand_smd1 el_one el_two el_three || p_code:, irr



* Model 5: Interactions with parties (think a little, which one to use)
nbreg pv sex##SDP sex##conserv sex##lib sex##tt sex##polish sex##dp sex##dk sex##lcs sex##tautp sex##greens revrank_be exp prev_tenures_excl minist_inc prev_min_excl cand_smd1 el_one el_two el_three i.p_code, irr
estat ic


* Investigating the differences in political experiance, making graphs for significant cases 
use "C:\Users\rragausk\Dropbox\Paper (Gender and politics)\Second RnR\New datasets to be merged\Gender updated 5.0 (2008-2016 stata) For graphs.dta", clear /*Change directory and dataset name */
collapse (mean) mean_prev_tenures = prev_tenures (sd) sd_prev_tenures=prev_tenures (count) n=prev_tenures (firstnm ) list_name , by(sex p_code)
generate hiprev_tenures =  mean_prev_tenures + invttail(n-1,0.025)*(sd_prev_tenures / sqrt(n))
generate lowprev_tenures =  mean_prev_tenures - invttail(n-1,0.025)*(sd_prev_tenures / sqrt(n))
graph bar  mean_prev_tenures if p_code==1 | p_code==2 | p_code==7 | p_code==25, over(sex)  over(p_code) asyvars /* do not include 25 (tautos prisikelimas), there is nothig there */
* Making so that I can fit error bars
generate sessex = sex if p_code == 1
replace sessex = sex+3 if p_code == 2
replace sessex = sex+6 if p_code == 3
replace sessex = sex+9 if p_code == 5
replace sessex = sex+12 if p_code == 6
replace sessex = sex+15 if p_code == 7
twoway (bar mean_prev_tenures sessex) (rcap hiprev_tenures lowprev_tenures sessex)
* Nice graph 
set scheme lean2
twoway (bar mean_prev_tenures sessex if p_code == 1) ///
       (bar mean_prev_tenures sessex if p_code == 2) ///
       (bar mean_prev_tenures sessex if p_code == 3) ///
       (bar mean_prev_tenures sessex if p_code == 5) ///
	   (bar mean_prev_tenures sessex if p_code == 6) ///
	   (bar mean_prev_tenures sessex if p_code == 7) ///
       (rcap hiprev_tenures lowprev_tenures sessex), ///
	   xlabel( 0.5 "SDP" 3.5 "Conservatives" 6.5 "Liberals" 9.5 "Ord. and Just." 12.5 "Labor Party" 15.5 "Lithuanian Poles", labsize(small)) ///
       subtitle("Average for All Candidates", span size(small)) ///
	   legend(off) saving(full_acutal)
     

	 
*********************************************************************
****************** Figure 2: Political experiance *******************
*********************************************************************
	 	 
* First part of the list
clear
set scheme lean2
use "C:\Users\rragausk\Dropbox\Paper (Gender and politics)\Second RnR\New datasets to be merged\Gender updated 5.0 (2008-2016 stata) For graphs.dta"
collapse (mean) mean_prev_tenures = prev_tenures (sd) sd_prev_tenures=prev_tenures (count) n=prev_tenures (firstnm ) list_name if stand_rank_be>=0.5, by(sex p_code)
generate hiprev_tenures =  mean_prev_tenures + invttail(n-1,0.025)*(sd_prev_tenures / sqrt(n))
generate lowprev_tenures =  mean_prev_tenures - invttail(n-1,0.025)*(sd_prev_tenures / sqrt(n))
generate sessex = sex if p_code == 1
replace sessex = sex+3 if p_code == 2
replace sessex = sex+6 if p_code == 3
replace sessex = sex+9 if p_code == 5
replace sessex = sex+12 if p_code == 6
replace sessex = sex+15 if p_code == 7
twoway (bar mean_prev_tenures sessex if p_code == 1) ///
       (bar mean_prev_tenures sessex if p_code == 2) ///
       (bar mean_prev_tenures sessex if p_code == 3) ///
       (bar mean_prev_tenures sessex if p_code == 5) ///
	   (bar mean_prev_tenures sessex if p_code == 6) ///
	   (bar mean_prev_tenures sessex if p_code == 7) ///
       (rcap hiprev_tenures lowprev_tenures sessex), ///
	   xlabel( 0.5 "SDP" 3.5 "Conservatives" 6.5 "Liberals" 9.5 "Ord. and Just." 12.5 "Labor Party" 15.5 "Lithuanian Poles", labsize(small)) ///
       subtitle("Average for Candidates in the First Half of the list", span size(small)) ///
	   legend(off) saving(full)

* First quartile
use "C:\Users\rragausk\Dropbox\Paper (Gender and politics)\Second RnR\New datasets to be merged\Gender updated 5.0 (2008-2016 stata) For graphs.dta", clear
collapse (mean) mean_prev_tenures = prev_tenures (sd) sd_prev_tenures=prev_tenures (count) n=prev_tenures (firstnm ) list_name if stand_rank_be>=0.75, by(sex p_code)
generate hiprev_tenures =  mean_prev_tenures + invttail(n-1,0.025)*(sd_prev_tenures / sqrt(n))
generate lowprev_tenures =  mean_prev_tenures - invttail(n-1,0.025)*(sd_prev_tenures / sqrt(n))
generate sessex = sex if p_code == 1
replace sessex = sex+3 if p_code == 2
replace sessex = sex+6 if p_code == 3
replace sessex = sex+9 if p_code == 5
replace sessex = sex+12 if p_code == 6
replace sessex = sex+15 if p_code == 7
twoway (bar mean_prev_tenures sessex if p_code == 1) ///
       (bar mean_prev_tenures sessex if p_code == 2) ///
       (bar mean_prev_tenures sessex if p_code == 3) ///
       (bar mean_prev_tenures sessex if p_code == 5) ///
	   (bar mean_prev_tenures sessex if p_code == 6) ///
	   (bar mean_prev_tenures sessex if p_code == 7) ///
       (rcap hiprev_tenures lowprev_tenures sessex), ///
	   xlabel( 0.5 "SDP" 3.5 "Conservatives" 6.5 "Liberals" 9.5 "Ord. and Just." 12.5 "Labor Party" 15.5 "Lithuanian Poles", labsize(small)) ///
       subtitle("Average for Candidates in the First Quartile of the list", span size(small)) ///
	   legend(off) saving(first_quart)
	   
graph combine full_acutal.gph full.gph first_quart.gph, col(1) iscale(1) ///
title("Figure 2: Mean Numb. of Times Candidate was Previously Elected to Parliament", span size(medium)) 
